from nicegui import persistence, ui, app
from db.bs_crud import create_base_station
from db.schemas.bs_mode_schemas import IBaseStationCreate


class AddBaseStationDialog:
    def __init__(self, db) -> None:
        """
        自定义增加基站Dialog
        """
        self.db = db
        # 使用 model_construct() 创建（跳过pydantic验证）
        self.basestation_create = IBaseStationCreate.model_construct()

    def show(self):
        with ui.dialog() as dialog, ui.card():
            with ui.card_section():
                ui.label("添加基站").classes("text-h6 w-[300px]")
            with ui.card_section().classes("q-pt-none"):
                with ui.column().classes("w-full"):
                    # 绑定输入框到 self.basestation_create 的字段
                    ui_bs_name = ui.input("1.基站名称").bind_value(
                        self.basestation_create, "name"
                    )
                    ui.label("2.基站经度")
                    ui_bs_type = ui.number(precision=6, placeholder="经度").bind_value(
                        self.basestation_create, "longitude"
                    )
                    ui.label("3.点位纬度")
                    ui_bs_latitude = ui.number(
                        precision=6, placeholder="纬度"
                    ).bind_value(self.basestation_create, "latitude")
                    ui.label("4.基站地址")
                    ui_bs_address = ui.input("4.基站地址").bind_value(
                        self.basestation_create, "address"
                    )
                    ui_bs_is_rooftop = ui.switch("5.是否天面").bind_value(
                        self.basestation_create, "is_rooftop"
                    )
            with ui.card_actions().classes("w-full justify-end"):
                ui.button("创建基站", on_click=lambda: self.add_bs()).props("flat")
                ui.button("取消", on_click=dialog.close).props("flat")
        dialog.open()

    def add_bs(self):
        if self.basestation_create.name:
            try:
                create_base_station(self.db, self.basestation_create)
                ui.notify("基站创建成功!", position="center", type="positive")
            except Exception as e:
                ui.notify(f"基站创建失败: {str(e)}", position="center", type="negative")
        else:
            ui.notify("基站名称不能为空!", position="center", type="warning")
        # 假设导航到基站管理页面，可根据实际情况调整
        ui.navigate.to("/mobile")
